#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
const int N=1005;
const int INF=0x3f3f3f3f;
struct Edge{
    int from,to,cap,flow,cost;
    Edge(int u,int v,int c,int f,int co):from(u),to(v),cap(c),flow(f),cost(co){}
};
int f;
vector<Edge> edges;
vector<int> G[N];
int a[N];
int p[N];
int l[N];
int r[N];
int w[N];
int tp[N];
int n,m,K,W;
void init(){
    for(int i=0;i<N;i++){
        G[i].clear();
    }
    memset(l,0,sizeof(l));
    memset(r,0,sizeof(r));
    memset(w,0,sizeof(w));
    memset(tp,0,sizeof(tp));
    edges.clear();
}
void addEdge(int from,int to,int cap,int cost){
    edges.push_back(Edge(from,to,cap,0,cost));
    edges.push_back(Edge(to,from,0,0,-cost));
    f=edges.size();
    G[from].push_back(f-2);
    G[to].push_back(f-1);
}
int d[N];
bool vis[N];
int cost;
int flow;
void spfa(int s,int t){
    queue<int> q;
    memset(vis,false,sizeof(vis));
    for(int i=0;i<N;i++){
        d[i]=INF;
    }
    d[s]=0;
    q.push(s);
    vis[s]=true;
    p[s]=0;
    a[s]=INF;
    while(!q.empty()){
        int v=q.front();
        vis[v]=false;
        q.pop();
        for(int i=0;i<G[v].size();i++){
            Edge &e=edges[G[v][i]];
            if(e.cap>e.flow && d[e.to]>d[v]+e.cost){
                d[e.to]=d[v]+e.cost;
                p[e.to]=G[v][i];
                a[e.to]=min(a[v],e.cap-e.flow);
                if(!vis[e.to]){
                    q.push(e.to);
                    vis[e.to]=true;
                }
            }
        }
    }
}
void mcmf(int s,int t){
    flow=0;
    cost=0;
    while(true){
        spfa(s,t);
        if(d[t]==INF){
            break;
        }
        flow+=a[t];
        cost+=d[t]*a[t];
        for(int u=t;u!=s;u=edges[p[u]].from){
            edges[p[u]].flow+=a[t];
            edges[p[u]^1].flow-=a[t];
        }
    }
}
int main(void){
    int t;
    scanf("%d",&t);
    while(t--){
        init();
        scanf("%d%d%d%d",&n,&m,&K,&W);
        for(int i=1;i<=m;i++){
            scanf("%d%d%d%d",&l[i],&r[i],&w[i],&tp[i]);
        }
        addEdge(0,2*m+1,K,0);
        for(int i=1;i<=m;i++){
            for(int j=1;j<=m;j++){
                if(i==j){
                    continue;
                }
                //两个视频可以连看
                if(r[i]<=l[j]){
                    addEdge(i+m,j,1,(tp[i]==tp[j]?W:0));
                }
            }
        }
        //拆点
        for(int i=1;i<=m;i++){
            addEdge(i,i+m,1,-w[i]);
        }
        //次源点与中间的连接
        for(int i=1;i<=m;i++){
            addEdge(2*m+1,i,1,0);
        }
        //中间与汇点的连接
        for(int i=1;i<=m;i++){
            addEdge(m+i,2*m+2,1,0);
        }
        mcmf(0,2*m+2);
        printf("%d\n",-cost);
    }
    return 0;
}